﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.TeamFoundation;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Framework.Client;
using Microsoft.TeamFoundation.Server;

namespace Sogeti.QueryWrapper
{
    public class RegistryWrapper
    {
     
        protected string teamProjectName;
       
        protected TfsTeamProjectCollection tpCollection;
        private IRegistration tfsRegistration;

        public RegistryWrapper(string theTpCollectionUrl, string theTeamProjectName)
        {
          
            teamProjectName=theTeamProjectName;
            tpCollection = new TfsTeamProjectCollection(new Uri(theTpCollectionUrl));
            tpCollection.EnsureAuthenticated();

            tfsRegistration = (IRegistration)tpCollection.GetService(typeof(IRegistration));
             

        }

        private string GetBaseSiteUrl()
        {
            return GetServiceUrlForTool(RegistrationUtilities.SharePointName, "BaseSiteUrl");
        }

        private string GetBaseAdminSiteUrl()
        {
            string adminServiceUrl = GetServiceUrlForTool(RegistrationUtilities.SharePointName, "WssAdminService");
            return adminServiceUrl.Substring(0, adminServiceUrl.IndexOf("/_vti_adm/"));
        }

        public string GetPortalUrl(string teamProject)
        {
                
            string portalType = GetTeamProjectProperty(teamProject, "PortalType");

            if (portalType == "WssSite")
            {
                return GetTeamProjectProperty(teamProject, "Portal") ;
            }
            else
            {
                /// The team project does not have a SharePoint site. 
                /// We return a dummy Url which will not be used.
                return GetBaseSiteUrl() + "/" + teamProject ;
            }
            
        }

        private string GetServiceUrlForTool(string tool, string name)
        {
            RegistrationEntry[] entries = tfsRegistration.GetRegistrationEntries(tool);

            foreach (RegistrationEntry entry in entries)
            {
                foreach (ServiceInterface sInterface in entry.ServiceInterfaces)
                {
                    if (String.Compare(sInterface.Name, name, true) == 0)
                    {
                        return sInterface.Url;
                    }
                }
            }

            return String.Empty;
        }

        private string GetTeamProjectProperty(string teamProject, string property)
        {
            // This call should be used only with Team Foundation Server 2010

            RegistrationEntry[] entries = tfsRegistration.GetRegistrationEntries("TeamProjects");

            foreach (RegistrationEntry entry in entries)
            {
                foreach (ServiceInterface sInterface in entry.ServiceInterfaces)
                {
                    if (teamProject == "*")
                    {
                        if (sInterface.Name.Contains(property))
                        {
                            return sInterface.Url;
                        }
                    }
                    else
                    {
                        if (String.Compare(sInterface.Name, String.Format("{0}:{1}", teamProject, property), true) == 0)
                        {
                            return sInterface.Url;
                        }
                    }
                }
            }

            return String.Empty;
        }
    }
}
